public class double_pointer_01 {
    public void duplicateZeros(int[] arr) {
        int count = 0;
        int len = arr.length;
        int cur = 0;
        for(cur = 0;cur<=len-1;cur++){
            if(count>=len){
                break;
            }
            if(arr[cur] == 0){
                count+=2;
            }else{
                count++;
            }
        }
        int dest = len-1;
        cur--;
        if(count == len+1){
            arr[dest--] = 0;
            cur--;
        }
        while(cur>=0&&dest>=0){
            if(arr[cur]!=0){
                arr[dest--] = arr[cur--];
            }else{
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }
        }
    }
    public static void moveZeroes(int[] nums) {
        int dest = 0;
        int cur = 0;
        while(cur<nums.length&&dest<=cur){
            while(nums[cur]==0&&dest<=cur){
                cur++;
                // 防止cur越界（当数组全为0时，cur会超过数组长度）
                if (cur >= nums.length) {
                    return;
                }
            }
            while(nums[dest]!=0 && dest<cur){
                dest++;
                if(dest>cur){
                    return;
                }
            }

            swap(nums,dest,cur);
            dest++;
            cur++;
        }
    }
    public void moveZeroes2(int[] nums){
        for(int cur = 0,dest = -1;cur < nums.length;cur++){
            if(nums[cur]!=0) {

                dest++;
                swap(nums, dest, cur);
            }
        }
    }
    public static void swap(int[] nums , int dest , int cur){
        int tmp = nums[dest];
        nums[dest] = nums[cur];
        nums[cur] = tmp;
    }

    public static void main(String[] args) {
        int[] nums = {0,1,0,3,12};
        //moveZeroes(nums);

        for (int x:nums
        ) {
            System.out.println(x);
        }
    }

}
